/*
	WebPAC AJAX class
*/

var WebPAC = { };

/*
	autocompleter which isn't (a suggest box)
	- don't just on first option on appear
	- removed fade-in/out effect
	- added configurable width (content width? centered?)
	- add icon which means suggest

	hopefully, this implementation won't break (much) when I update
	prototype next time :-)
*/


WebPAC.Suggest = Class.create();
Logger.debug('Created WebPAC.Suggest Class');
Object.extend(Object.extend(WebPAC.Suggest.prototype, Ajax.Autocompleter.prototype), {
  initialize: function(element, update, url, options) {
    this.baseInitialize(element, update, options);
    this.options.asynchronous  = true;
    this.options.onComplete    = this.onComplete.bind(this);
    this.options.defaultParams = this.options.parameters || null;
    this.url                   = url;
    this.ignoreReturn = true;

    Logger.debug('WebPAC.Sugget initialize '+element+','+update+','+url);

    this.options.onShow = function(element, update) {
      if(!update.style.position || update.style.position=='absolute') {
        update.style.position = 'absolute';
        Position.clone(element, update, {setHeight: false, offsetTop: element.offsetHeight});
      }
      new Element.show(update);
      Logger.debug('WebPAC.Suggest.onShow');
    };

    this.options.onHide = function(element, update) {
    	new Element.hide(update);
	Logger.debug('WebPAC.Suggest.onHide');
    };
  },

  getUpdatedChoices: function() {
    var entry = encodeURIComponent(this.options.paramName) + '=' +
      encodeURIComponent(this.getToken());

    this.options.parameters = this.options.callback ?
      this.options.callback(this.element, entry) : entry;

    if(this.options.defaultParams)
      this.options.parameters += '&' + this.options.defaultParams;

    var form = $('results_form');
    var inputs = form.getElementsByTagName('input');
    var name = '_database';

    var matchingInputs = new Array();
    for (var i = 0; i < inputs.length; i++) {
      var input = inputs[i];
      if (input.name != name || !input.checked) continue;
      Logger.debug('keeping '+name+'='+input.value);
      matchingInputs.push(name+'='+input.value);
    }

    // construct arguments for url
    var args = '';
    var pos = this.url.indexOf('?');
    if (pos == -1) {
      args += '?';
    } else {
      args += '&';
    }
    args += matchingInputs.join('&');

    Logger.debug('WebPAC.Suggest called '+this.url+args);
    new Ajax.Request(this.url+args, this.options);
  },

  onComplete: function(request) {
    Logger.debug('WebPAC.Suggest onComplete called');
    this.updateChoices(request.responseText);
  },

  markPrevious: function() {
    if(this.index > 0) this.index--
    //  else this.index = this.entryCount-1;
  },

  markNext: function() {
    if(this.index < this.entryCount-1) this.index++
    //  else this.index = 0;
  }

});

/*
	WebPAC.Updater
*/

WebPAC.Updater = Class.create();
Logger.debug('Created WebPAC.Updater Class');
Object.extend(WebPAC.Updater.prototype, {
  initialize: function(container, url, options) {
    Logger.debug('WebPAC.Updater initialize '+container+','+url+','+options);
    if (url.indexOf('/ajax') == -1) {
      var pos = url.indexOf('?');
      if (pos == -1) {
        url += '/ajax';
      } else {
        url = url.substring(0,pos) + '/ajax' + url.substring(pos);
      }
      Logger.debug('added /ajax to '+url+' pos:'+pos);
    }
    Logger.info('using AJAX to update "'+container+'" from '+url);
    this.updater = new Ajax.Updater(container, url, options);
  }
});

